package cn.anecansaitin.firecrafting.common.block;

import cn.anecansaitin.firecrafting.common.block.entity.FireInterfaceEntity;
import cn.anecansaitin.firecrafting.common.block.entity.ModBlockEntityTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.network.NetworkHooks;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

//火焰接口
public class FireInterface extends Block implements EntityBlock {
    //定义光照属性
    public static final BooleanProperty LIT = BlockStateProperties.LIT;
    //添加朝向属性
    public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;

    public FireInterface() {
        //构造方块属性
        super(BlockBehaviour.Properties
                .of(Material.STONE)
                .lightLevel((b) -> b.getValue(LIT) ? 15 : 0)
                .sound(SoundType.STONE)
                .strength(3.0F, 3.0F)
                .requiresCorrectToolForDrops());
        //将自定义属性加入默认属性中
        registerDefaultState(defaultBlockState().setValue(LIT, Boolean.FALSE).setValue(FACING, Direction.NORTH));
    }

    //给方块添加属性
    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
        builder.add(LIT)
                .add(FACING);
    }

    //当方块被放置时触发
    @Nullable
    @Override
    public BlockState getStateForPlacement(BlockPlaceContext context) {
        //获取默认state
        BlockState state = defaultBlockState();
        //获取放置下方一格坐标
        BlockPos pos = context.getClickedPos().below();
        //获取世界
        Level world = context.getLevel();
        //判断坐标位置火焰
        if (world.getBlockState(pos).getBlock() instanceof BaseFireBlock) {
            //设置LIT为true
            state = state.setValue(LIT, true);
        }
        //设置方向
        state = state.setValue(FACING, context.getHorizontalDirection().getOpposite());
        //返回state
        return state;
    }

    //为方块添加BlockEntity
    @Nullable
    @Override
    public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState state) {
        return new FireInterfaceEntity(pos, state);
    }

    //设置BlockEntity为每tick都需要调用
    @Nullable
    @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level world, @NotNull BlockState state, @NotNull BlockEntityType<T> type) {
        if (world.isClientSide() || type != ModBlockEntityTypes.FIRE_INTERFACE_ENTITY.get()) {
            return null;
        }

        return (w, p, s, e) -> ((FireInterfaceEntity)e).tick(w, p, s);
    }

    //设置方块旋转
    @Override
    public BlockState rotate(BlockState state, LevelAccessor level, BlockPos pos, Rotation direction) {
        return state.setValue(FACING, direction.rotate(state.getValue(FACING)));
    }

    @Override
    public @NotNull BlockState mirror(BlockState state, Mirror mirror) {
        return state.rotate(mirror.getRotation(state.getValue(FACING)));
    }

    //当右击时行为
    @Override
    public @NotNull InteractionResult use(@NotNull BlockState state, @NotNull Level world, @NotNull BlockPos pos, Player player, @NotNull InteractionHand hand, @NotNull BlockHitResult hit) {
        //潜行状态
        if (player.isShiftKeyDown()) {
            //返回跳过
            return InteractionResult.PASS;
        }
        //获取方块实体
        BlockEntity blockEntity = world.getBlockEntity(pos);
        if (!world.isClientSide && blockEntity instanceof FireInterfaceEntity) {
            //打开gui
            NetworkHooks.openGui((ServerPlayer) player, (MenuProvider) blockEntity, pos);
        }

        return InteractionResult.SUCCESS;
    }

    //破坏后物品掉落
    @Override
    public void onRemove(@NotNull BlockState state, Level world, @NotNull BlockPos pos, @NotNull BlockState newState, boolean isMoving) {
        if (!state.is(newState.getBlock())) {
            BlockEntity blockEntity = world.getBlockEntity(pos);
            if (!(blockEntity instanceof FireInterfaceEntity)) {
                return;
            }
            Containers.dropContents(world, pos, ((FireInterfaceEntity) blockEntity).getItemIn().getItems());
            ItemStackHandler itemOut = ((FireInterfaceEntity) blockEntity).getItemOut();
            for (int i = 0; i < itemOut.getSlots(); i++) {
                Containers.dropItemStack(world, pos.getX(), pos.getY(), pos.getZ(), itemOut.getStackInSlot(i));
            }
            world.removeBlockEntity(pos);
        }
    }
}
